package com.jk.smartelderlycare.controller;

import com.jk.smartelderlycare.dto.SysRole;
import com.jk.smartelderlycare.dto.SysUser;
import com.jk.smartelderlycare.pojo.PagedResponse;
import com.jk.smartelderlycare.pojo.ResponseResult;
import com.jk.smartelderlycare.security.LoginUser;
import com.jk.smartelderlycare.service.SysRoleServiceFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author hhk
 * @version 1.0
 * @ClassName SysRoleController
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/11/7 14:27
 */
@Api(tags = "角色管理模块")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping(value = "sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleServiceFeign sysRoleServiceFeign;
    /**
     * @description: 角色列表后端分页查询
     * @param: [sysRole, pageNum, pageSize]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<com.jk.smartelderlycare.pojo.PagedResponse<com.jk.smartelderlycare.dto.SysRole>>
     * @author hhk
     * @date: 2024/11/7 14:42
     */
    @ApiOperation(value = "角色后端分页查询",httpMethod = "POST",notes = "角色后端分页查询")
    @PostMapping(value = "findSysSysRolePaginationList")
    public ResponseResult<PagedResponse<SysRole>>findSysSysRolePaginationList(@RequestBody SysRole sysRole, @RequestParam(value = "pageNum") Integer pageNum,@RequestParam(value = "pageSize") Integer pageSize){
        return new ResponseResult<>(200,"查询成功",sysRoleServiceFeign.findSysSysRolePaginationList(sysRole,pageNum,pageSize));
    }
    /**
     * @description: 新增角色列表
     * @param: [sysRole]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/7 14:41
     */
    @ApiOperation(value = "新增角色列表",httpMethod = "POST",notes = "新增角色列表")
    @PostMapping(value = "addSysRole")
    private ResponseResult addSysRole(@RequestBody SysRole sysRole){
        try {
            sysRoleServiceFeign.addSysRole(sysRole);
            return new ResponseResult(200,"新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"新增失败");
        }
    }
    /**
     * @description: 根据id删除一条数据
     * @param: [roleId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/7 14:45
     */
    @ApiOperation(value = "根据id删除一条数据",httpMethod = "DELETE",notes = "根据id删除一条数据")
    @DeleteMapping(value = "deleteSysRoleByRoleId")
    private ResponseResult deleteSysRoleByRoleId(@RequestParam(value = "roleId") Long roleId){
        try {
            sysRoleServiceFeign.deleteSysRoleByRoleId(roleId);
            return new ResponseResult(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"删除失败");
        }
    }
    /**
     * @description: 根据id回显一条数据
     * @param: [roleId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<com.jk.smartelderlycare.dto.SysRole>
     * @author hhk
     * @date: 2024/11/7 14:48
     */
    @ApiOperation(value = "根据id回显一条数据",httpMethod = "GET",notes = "根据id回显一条数据")
    @GetMapping(value = "findSysRoleByRoleId")
    private ResponseResult<SysRole>findSysRoleByRoleId(@RequestParam(value = "roleId") Long roleId){
       return new  ResponseResult<>(200,"回显成功",sysRoleServiceFeign.findSysRoleByRoleId(roleId));
    }
    /**
     * @description: 修改角色列表
     * @param: [sysRole]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/7 14:52
     */
    @ApiOperation(value = "修改角色列表",httpMethod = "PUT",notes = "修改角色列表")
    @PutMapping(value = "updateSysRole")
    private ResponseResult updateSysRole(@RequestBody SysRole sysRole){
        try {
            //获取当前登陆人赋修改时间与修改人
            LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            sysRole.setModifyDate(new Date());
            sysRole.setModifyUser(loginUser.getUser().getUserName());
            sysRoleServiceFeign.updateSysRole(sysRole);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }
    }
    /**
     * @description: 状态修改
     * @param: [sysRole]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/7 16:50
     */
    @ApiOperation(value = "状态修改",httpMethod = "POST",notes = "状态修改")
    @PostMapping(value = "updateStatusById")
    public ResponseResult updateStatusById(@RequestBody SysRole sysRole) {
        try {
            sysRoleServiceFeign.updateStatusById(sysRole);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }
    }









}
